package com.zyj.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zyj.dao.CategoryDao;
import com.zyj.pojo.Category;

@Service
@Transactional
public class CategoryService {
	
	private CategoryDao categoryDao;

	public List<Category> findAll() {
		return categoryDao.findAll();
	}
	public List<Category> fingByParentId(String pId) {
		int p = Integer.parseInt(pId);
		return categoryDao.fingByParentId(p);
	}

	public Category findById(String id) {
		int i = Integer.parseInt(id);
		return categoryDao.findById(i);
	}
	public void rename(String id, String categoryname) {
		Category category = findById(id);
		category.setCategoryname(categoryname);
		categoryDao.save(category);
	}
	public void remove(String id) {
		int i = Integer.parseInt(id);
		categoryDao.del(i);
	}
	public Category save(String categoryname, String parentid) {
		int i = Integer.parseInt(parentid);
		Category category1 = categoryDao.findById(i);
		
		Category category = new Category();
		category.setCategoryname(categoryname);
		category.setParent(category1);
		category.setRoom(category1.getRoom());
		
		categoryDao.save(category);
		
		return category;
	}
	public void drag(String id, String parentid) {
		Category category = findById(id);
		Category categoryParent = findById(parentid);
		
		category.setParent(categoryParent);
		category.setRoom(categoryParent.getRoom());
		
		categoryDao.save(category);
		
	}
	
	@Autowired
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	
	
	
	
}
